<?php
$allCategory = $this->allCategory;
?>

<div class="ad_title_ctn">
    <h2>Quản lý thực đơn chung</h2>
</div>

<div class="ad_content_ctn">
    <div class="dish_critetia_ctn">
        <div class="left" style="width:300px">
            <span>Nhóm chính:</span>
            <select id="cboPrimaryCategory">
                <option value="ALL">All</option>
                <?php
                foreach ($allCategory as $aCategory) {
                    echo '  <option value="'.$aCategory["id"].'">'.$aCategory["name"].'</option>';
                }

                ?>
            </select>
        </div>
        


        <div class="clear"></div>
        <div style="margin: 10px 0px">
            <span>Tên món: </span>
            <input class="k-textbox" style="width: 300px" id="txtName" >
        </div>

    </div>
    <div class="dish_search_ctr">
        <a id="btnSearch"  class="k-button k-button-icontext">Tìm kiếm</a>
    </div>
    <div id="dish_grid"></div>
    <div id="details"></div>
</div>

<script type="text/x-kendo-template" id="template">
    <div class="toolbar">
        <a class="k-button k-button-icontext  k-grid-edit"
           href="/admin/dish/add">
            <span class="k-icon k-add"></span>Tạo mới
        </a>
    </div>
</script>

<script type="text/x-kendo-template" id="detailTemplate">
    <div id="details-container">
        <h3>#= name #</h3>
        <em>#= description #</em>
        <dl>
            <dt>Nhóm chính: #= primary_category #</dt>
            <dt>Nhóm phụ: #= sub_category #</dt>
            <dt>Tag:        #= tag #</dt>
        </dl>
    </div>
</script>

<script type="text/javascript">
    var wnd,
        detailsTemplate;
    $(document).ready(function() {
        $(".dish_critetia_ctn").keyup( function(event) {
            if ( event.which == 13 ) {
                event.preventDefault();
                doSearch();
            }
        });

        $("#cboPrimaryCategory").kendoComboBox({
            filter: "contains",
            suggest: true,
            index: 3
        });

        var cboPrimaryCategory = $("#cboPrimaryCategory").data("kendoComboBox");

        dataSource = new kendo.data.DataSource({
            type: "jsonp",
            transport: {

                read: function(options) {
                    options.data.name=$('#txtName').val();
                    options.data.category=cboPrimaryCategory.value();

                    $.ajax( {
                        url: "<?php echo '/admin/dish/list'  ?>",
                        data: options.data, // the "data" field contains paging, sorting, filtering and grouping data
                        success: function(result) {
                            options.success(result);
                        },
                        dataType: "json",
                        type: "POST"

                    });
                },



                destroy: function(options) {
                    $.ajax( {
                        url: "/admin/dish/remove?id="+options.data.id,
                        data: options.data,
                        success: function(result) {
                            if(result.success){
                                options.success(options.data.models);
                                $('#error_ctn').addClass('blue_text').html('Xóa thành công.');
                            }else{
                                $('#error_ctn').removeClass('blue_text').html(result.msg);
                                grid = $("#dish_grid").data("kendoGrid");
                                // cancel changes
                                grid.cancelChanges();
                            }


                        },
                        dataType: "json",
                        type: "POST"

                    });
                }
            },
            schema: {
                model: {
                    id: "id",
                    fields: {
                        id: {editable: false,  type: "number" },
                        name: { type: "string" },
                        active: { type: "number" }
                    }
                },
                data: "rows",
                total: "total"
            },
//                    batch: true,
            pageSize: 30,
            serverPaging: true,
            serverFiltering: true,
            serverSorting: true
        });

        $("#dish_grid").kendoGrid({
            dataSource: dataSource,
            navigatable: true,
            pageable: true,

            columnMenu: true,
            sortable: true,
            resizable:true,
            toolbar:kendo.template($("#template").html()),

            columns: [
                { field: "name", title: "Tên món", width: 300, template:" <a href='<?php echo '/admin/dish/add?id='  ?>#= id #'>#= name #</a> " },
                { field: "active", title: "Active", width: 40,
                    template: "<input onclick='doActive(this, \"#= id#\")'  disabled type='checkbox' data-bind='checked: 1' #= active ? checked='checked' : '' #/>"},
                { command:  [{name: "destroy", text: ""},
                    { text: "Chi tiết", click: showDetails }],  title: "&nbsp;", width: 80 }

            ],
            editable: "inline"
        });

        wnd = $("#details")
            .kendoWindow({
                title: "Thông tin món ăn",
                modal: true,
                visible: false,
                resizable: false,
                width: 500
            }).data("kendoWindow");

        detailsTemplate = kendo.template($("#detailTemplate").html());
        function showDetails(e) {
            e.preventDefault();
            dataItem = this.dataItem($(e.currentTarget).closest("tr"));
            id = dataItem.id;
            $.post('/admin/dish/get-dish-for-view',{id:id}
                ,function(result){
                    if (result.success){

                        newDataItem ={name:result.name,
                            description:result.description,
                            primary_category:result.primary_category,
                            sub_category:result.sub_category,
                            tag:result.tag};

                        wnd.content(detailsTemplate(newDataItem));
                        wnd.title(result.header);
                        wnd.center().open();

                    }else{

                    }

                },'json');


        }

        function doSearch(){
            grid = $("#dish_grid").data("kendoGrid");
            grid.dataSource.page(1);
            grid.dataSource.read();
        }

        $('#btnSearch').click(function(){
            doSearch();
        })
    });

</script>